let { token } = require("./token_schema.js");
let crypto = require("crypto");
function Deencrypt(data) {
    const key = Buffer.from('9vApxLk5G3PAsJrM', 'utf-8');
    const iv = Buffer.from('FnJL7EDzjqWjcaY9', 'utf-8');
    let src = '';
    const cipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
    src += cipher.update(data, 'hex', 'utf8');
    src += cipher.final('utf8');
    return src;
}
module.exports = {
    findToken: async function (tokenData) {
        
        console.log(tokenData, 111);
        let sult = Deencrypt(tokenData);
        let tokenArr = sult.split("-");
        let result = await token.find({ user: tokenArr[1], token: tokenData }).then((data) => {
            if (data.length > 0) {
                let userToken = data[0];
                // let tokenOnline = new Date("2021-08-30 14:15:54").getTime() + Number(userToken.outTime);
                let tokenOnline = new Date(userToken.createTime).getTime() + Number(userToken.outTime);
                let newDate = new Date().getTime();
                console.log(new Date(tokenOnline), new Date(newDate));

                if (tokenOnline > newDate) {
                    return Promise.resolve(JSON.stringify({
                        code: 1000,
                        msg: "token验证通过"
                    }))
                } else {
                    return Promise.resolve(JSON.stringify({
                        code: 1002,
                        msg: "token已过期、请重新登录"
                    }))
                }
            } else {
                return Promise.resolve(JSON.stringify({
                    code: 1001,
                    msg: "token不存在"
                }))
            }

        })
        return result;
    }
}